﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AGDN;
using System;

namespace Sudum
{
    class PlayerShip
    {
        public PlayerShip()
        {
            initialized = false;
        }

        void CheckPlayerMovement()
        {
            // Polyline Phase is in range [0:1] for the whole tube boundaries
            if (InputManager.Me.IsDown(Action.Right))
                currentPolylinePhase -= kShipMoveSpeed;

			if (InputManager.Me.IsDown(Action.Left))
                currentPolylinePhase += kShipMoveSpeed;

            // Wrap phase to be always in [0,1)
            currentPolylinePhase = currentPolylinePhase - (float)Math.Floor(currentPolylinePhase);
            if (currentPolylinePhase < 0.0f)
                currentPolylinePhase += 1.0f;
        }

        public void Update()
        {
            if (!initialized)
                return;

            CheckPlayerMovement();
            CheckPlayerFiring();
        }

        void CheckPlayerFiring()
        {
            nextFiringTime -= kShipMoveSpeed;

			if (InputManager.Me.IsDown(Action.Fire))
                Fire();
        }

        public void Draw()
        {
            if (!initialized)
                return;

            //
            // Draw using loaded model
            //

            // Copy any parent transforms.
            Matrix[] transforms = new Matrix[vehicleModel.Bones.Count];
            vehicleModel.CopyAbsoluteBoneTransformsTo(transforms);

            // Draw the model. A model can have multiple meshes, so loop.
            foreach (ModelMesh mesh in vehicleModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.VertexColorEnabled = true;
                    
                    Vector3 n = Vector3.Zero;
                    Vector3 pos = parentLevel.GetPositionFromPolyline(currentPolylinePhase, ref n);

                    // apply world transformation here
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(pos) * Matrix.CreateRotationX(parentLevel.GetTubeRotationX());

                    // set current view/projection
                    effect.View = parentLevel.GetLevelCamera().GetView();
                    effect.Projection = parentLevel.GetLevelCamera().GetProjection();
                }

                // Draw the mesh, using the effects set above.
                mesh.Draw();
            }
        }

        void Fire()
        {
            if (!initialized)
                return;

            if (nextFiringTime > 0)
                return;

            // create bullet here
            Vector3 normal = new Vector3();
            Vector3 p = parentLevel.GetPositionFromPolyline(currentPolylinePhase, ref normal);
            BulletManager.Me.CreateBullet(p, normal, BulletSource.Player);

            // allow next fire after some time
            nextFiringTime = kFireInterval;
        }

        public void Init(GameLevel pLevel)
        {
            // load vehicle model
            vehicleModel = Sudum.Me.Content.Load<Model>("Models\\Cube");
            parentLevel = pLevel;

            initialized = true;
        }        

        Model vehicleModel;
        bool initialized;
        float currentPolylinePhase = 0.5f;
        const float kShipMoveSpeed = 0.6f * 0.016f;
        GameLevel parentLevel;
        float nextFiringTime;
        const float kFireInterval = 0.2f;   // we can fire once each 0.2 seconds
    }
}
